// 🎯 S4 策略：连错淘汰机制（ConsecutiveMissEliminator）
// 根据每个号码的连续错误次数，淘汰那些长期不中的号码

const _ = require("lodash");

function updateConsecutiveMiss(missCountMap, predictedNumbers, actualNumber) {
  predictedNumbers.forEach((num) => {
    if (num !== actualNumber) {
      missCountMap[num] = (missCountMap[num] || 0) + 1;
    } else {
      missCountMap[num] = 0;  // 如果命中，重置该号码的连续不中次数
    }
  });

  return missCountMap;
}

// 根据动态 missThreshold（连续错的次数阈值）来决定是否淘汰号码
function select32ByS4(temp, missCountMap, missThreshold = 5) {
  const allNumbers = Array.from({ length: 49 }, (_, i) =>
    (i + 1).toString().padStart(2, '0')
  );

  const filteredNumbers = allNumbers.filter((num) => missCountMap[num] <= missThreshold);

  // 如果过滤后的号码少于 32 个，则补足
  const result = _.uniq([...filteredNumbers, ..._.shuffle(allNumbers)]).slice(0, 32);

  return result;
}

module.exports = { select32ByS4, updateConsecutiveMiss };
